/**
 * meilele.com Inc.
 * Copyright (c) 2004-2016 All Rights Reserved.
 */
package com.gpagers.ui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.gpagers.core.ObjectRef;

/**
 * 
 * @author liuzijie@meilele.com
 * @version $Id: EditNewTaskUi.java, v 0.1 2016年5月9日 下午5:36:48 liuzijie@meilele.com Exp $
 */
public class EditNewTaskUi extends Shell {
    private Text  text;
    private Text  text_1;
    private Label lblNewLabel;
    private Label lblNewLabel_1;

    /**
     * Create the shell.
     * @param display
     */
    public EditNewTaskUi(Shell shell, final Task task, final int index, String name,
                         String expression) {
        super(shell, SWT.CLOSE | SWT.RESIZE | SWT.TITLE | SWT.SYSTEM_MODAL);
        final boolean create = (index == -1 ? true : false);
        setLayout(new FormLayout());
        this.setImage(new Image(ObjectRef.display, ObjectRef.imagePath + "images/add.png"));
        Label label = new Label(this, SWT.NONE);
        FormData fd_label = new FormData();
        fd_label.top = new FormAttachment(0, 15);
        label.setLayoutData(fd_label);
        label.setText("名字 ： ");

        Label label_1 = new Label(this, SWT.NONE);
        fd_label.right = new FormAttachment(label_1, 0, SWT.RIGHT);
        FormData fd_label_1 = new FormData();
        fd_label_1.top = new FormAttachment(label, 20);
        fd_label_1.left = new FormAttachment(0, 10);
        label_1.setLayoutData(fd_label_1);
        label_1.setText("表达式 ： ");

        text = new Text(this, SWT.BORDER);
        text.setText(name);
        FormData fd_text = new FormData();
        fd_text.left = new FormAttachment(label, 6);
        fd_text.top = new FormAttachment(0, 12);
        text.setLayoutData(fd_text);
        text.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent arg0) {
                if (checkName(text.getText())) {
                    lblNewLabel.setImage(
                        new Image(ObjectRef.display, ObjectRef.imagePath + "images/correct.png"));
                } else {
                    lblNewLabel.setImage(
                        new Image(ObjectRef.display, ObjectRef.imagePath + "images/error.png"));
                }
            }
        });

        text_1 = new Text(this, SWT.BORDER);
        text_1.setText(expression);
        FormData fd_text_1 = new FormData();
        fd_text_1.top = new FormAttachment(text, 14);
        fd_text_1.left = new FormAttachment(label_1, 6);
        text_1.setLayoutData(fd_text_1);
        text_1.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent arg0) {
                if (checkExpression(text_1.getText())) {
                    lblNewLabel_1.setImage(
                        new Image(ObjectRef.display, ObjectRef.imagePath + "images/correct.png"));
                } else {
                    lblNewLabel_1.setImage(
                        new Image(ObjectRef.display, ObjectRef.imagePath + "images/error.png"));
                }
            }
        });

        Button btnNewButton = new Button(this, SWT.NONE);
        FormData fd_btnNewButton = new FormData();
        fd_btnNewButton.left = new FormAttachment(0, 71);
        fd_btnNewButton.right = new FormAttachment(100, -62);
        fd_btnNewButton.top = new FormAttachment(text_1, 22);
        btnNewButton.setLayoutData(fd_btnNewButton);
        btnNewButton.setText("确认");
        btnNewButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                if (!checkName(text.getText())) {
                    lblNewLabel.setImage(
                        new Image(ObjectRef.display, ObjectRef.imagePath + "images/error.png"));
                }
                if (!checkExpression(text_1.getText())) {
                    lblNewLabel_1.setImage(
                        new Image(ObjectRef.display, ObjectRef.imagePath + "images/error.png"));
                }
                if (!checkName(text.getText()) || !checkExpression(text_1.getText())) {
                    return;
                }
                if (create) {
                    task.createTableItem(text.getText(), text_1.getText(), ObjectRef.PAUSED);
                } else {
                    task.editTableItem(index, text.getText(), text_1.getText());
                }
                ObjectRef.currentEditorTask.dispose();
            }
        });

        lblNewLabel = new Label(this, SWT.NONE);
        fd_text.right = new FormAttachment(100, -61);
        FormData fd_lblNewLabel = new FormData();
        fd_lblNewLabel.top = new FormAttachment(label, 0, SWT.TOP);
        fd_lblNewLabel.left = new FormAttachment(text, 13);
        lblNewLabel
            .setImage(new Image(ObjectRef.display, ObjectRef.imagePath + "images/correct.png"));
        lblNewLabel.setLayoutData(fd_lblNewLabel);

        lblNewLabel_1 = new Label(this, SWT.NONE);
        fd_text_1.right = new FormAttachment(100, -61);
        FormData fd_lblNewLabel_1 = new FormData();
        fd_lblNewLabel_1.top = new FormAttachment(label_1, 0, SWT.TOP);
        fd_lblNewLabel_1.left = new FormAttachment(text_1, 13);
        lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
        lblNewLabel_1
            .setImage(new Image(ObjectRef.display, ObjectRef.imagePath + "images/correct.png"));
        createContents();
    }

    private boolean checkName(String name) {
        return "".equals(name) ? false : true;
    }

    private boolean checkExpression(String expression) {
        String rex = "^(([0-9]+((-|\\/|,)[0-9]+)?|\\*)\\s){6,6}$";
        Pattern pt3 = Pattern.compile(rex);
        Matcher mt3 = pt3.matcher(expression);
        //return mt3.matches();
        return "".equals(expression) ? false : true;
    }

    /**
     * Create contents of the shell.
     */
    protected void createContents() {
        setText("添加任务");
        setSize(314, 181);
    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }
}
